// 全局变量
var today = new Date();
var todayTxt = today.format('yyyy-MM-dd');

var MY_SEWAGE_TEST_REPORT = (function() {

	
	// 收集页面查询参数
	function getParams(){
		
		var data = {};
		
		// 标本日期 from - to
		var $dtFrom = $('#dtFrom');
		if ( !$dtFrom.val().length ){
			WeKnow.errorMsg('请输入查询开始日期');
			$dtFrom.focus();
			return false;
		}
		
		var dtFrom = $dtFrom.val();
		if ( !MY_VALIDATOR.isDate(dtFrom,'yyyy-MM-dd')){
			WeKnow.errorMsg('查询开始日期格式不正确');
			$dtFrom.focus();
			return false;
		}
		data.dtFrom = dtFrom;
		
		// 查询日期to
		var $dtTo = $('#dtTo');
		if ( !$dtTo.val().length ){
			WeKnow.errorMsg('请输入查询结束日期');
			$dtTo.focus();
			return false;
		}
		
		var dtTo = $dtTo.val();
		if ( !MY_VALIDATOR.isDate(dtTo,'yyyy-MM-dd')){
			WeKnow.errorMsg('查询结束日期格式不正确');
			$dtTo.focus();
			return false;
		}
		data.dtTo = dtTo;
        if($dtFrom.val()>$dtTo.val()){
            WeKnow.errorMsg('开始时间不能大于结束时间');
            return false;
        }
		// 异常状态
		var checkSt =  $('#checkSt').val();
		if ( checkSt != '9' ){
			data.checkSt = checkSt;
		}
		
		// 诊所名
		var institutionName = $.trim( $('#institutionName').val());
		if ( institutionName.length ){
			data.institutionName = institutionName;
		}
		// 分类 1:污水检测报告 2:灭菌效果生物监测报告
		data.businessSubType = $('#businessSubType').val();
		if ( $('#onlyShowAppeal').prop('checked') ){
			data.onlyShowAppeal = 1;//只显示
		}
		if ($("#operateType").val()==1||$("#operateType").val()==3){
			data.dateYm=$("#year").val()+"-"+$("#month").val();
		}

		data.institutionTypeFirst = $('select[name="institutionTypeFirstScreen"]').val();//医疗机构类别

		return data;
	}
	
	return {
		getParams:getParams
	}
})();

$(document).ready(function() {
	
	var $thead = $('#test_report_thead');
	var $tbody = $('#test_report_tbody');
	// var $tbodyReport = $('#sewage_report');
    var ACTIVIE_CLASS = 'active_report_tab';
	//查询按钮按下
	$('#search_test').on('click',function(){
        var cur_tab = $('.active_report_tab').prop('id');
        $('.list-section').hide();
		var params = MY_SEWAGE_TEST_REPORT.getParams();
		//清除排序
		var  oTable = document.getElementById("dataTable");
		oTable.sortCol = -1;
		//重置全部排序图标
		$("after").css("opacity",0.3);
		$("before").css("opacity",0.3);
		if(params) {
            // MY_SEWAGE_TEST_REPORT_COM.loadItem(params, $thead, $tbody);
            if ( cur_tab == 'instrument_disinfect' ){//消毒器械检测报告
				$(".date2").show();
				$(".date").hide();
                $('#operateType').val('1');
                $('#businessSubType').val('303');
                var params = MY_SEWAGE_TEST_REPORT.getParams();
                MY_SEWAGE_TEST_REPORT_COM.loadItem(params,$thead,$tbody);
                //过滤框显示变换
                $('#test-report-area').show();//报告section显示
            } else if ( cur_tab == 'sewage_chlorine' ){//污水余氯
				$(".date").show();
				$(".date2").hide();
                $('#operateType').val('2');
                MY_SEWAGE.loadItem();

                $('#sewage-chlorine').show();//余氯section显示
            } else if ( cur_tab == 'sewage_report' ){//污水报告
				$(".date2").show();
				$(".date").hide();
                $('#operateType').val('3');
                $('#businessSubType').val('301');//查询值
                var params = MY_SEWAGE_TEST_REPORT.getParams();
                MY_SEWAGE_TEST_REPORT_COM.loadItem(params,$thead,$tbody);
                $('#test-report-area').show();//报告section显示
            }
        }
	});

	// 设备消毒查询 、场所消毒查询
	$('.report_tab').on('click',function(){
		
		var $that = $(this);
		// 切换tab选中状态
		$('.report_tab').removeClass(ACTIVIE_CLASS);
		$that.addClass(ACTIVIE_CLASS);
		//清除排序
		var  oTable = document.getElementById("dataTable");
		oTable.sortCol = -1;
		//重置全部排序图标
		$("after").css("opacity",0.3);
		$("before").css("opacity",0.3);
		
		var cur_tab = $that.prop('id');
		lCurTab = cur_tab;//保存给公用下拉框更新使用
		
		$('.list-section').hide();//先隐藏所有list区，然后根据点的tab来显示指定内容
		
		if ( cur_tab == 'instrument_disinfect' ){//消毒器械检测报告
			$(".date2").show();
			$(".date").hide();
			$('#operateType').val('1');
			$('#businessSubType').val('303');
			var params = MY_SEWAGE_TEST_REPORT.getParams();
			MY_SEWAGE_TEST_REPORT_COM.loadItem(params,$thead,$tbody);
			//过滤框显示变换
			$('#test-report-area').show();//报告section显示
		} else if ( cur_tab == 'sewage_chlorine' ){//污水余氯
			$(".date2").hide();
			$(".date").show();
			$('#operateType').val('2');
			MY_SEWAGE.loadItem();
			
			$('#sewage-chlorine').show();//余氯section显示
		} else if ( cur_tab == 'sewage_report' ){//污水报告
			$(".date2").show();
			$(".date").hide();
			$('#operateType').val('3');
			$('#businessSubType').val('301');//查询值 
			var params = MY_SEWAGE_TEST_REPORT.getParams();
			MY_SEWAGE_TEST_REPORT_COM.loadItem(params,$thead,$tbody);
			$('#test-report-area').show();//报告section显示
		}
	});
	var lCurTab = "";

	$('select[name="institutionTypeFirstScreen"]').on('change',function(){
		if ( lCurTab == 'instrument_disinfect' ){//消毒器械检测报告
			var params = MY_SEWAGE_TEST_REPORT.getParams();
			MY_SEWAGE_TEST_REPORT_COM.loadItem(params,$thead,$tbody);
		} else if ( lCurTab == 'sewage_chlorine' ){//污水余氯
			MY_SEWAGE.loadItem();
		} else if ( lCurTab == 'sewage_report' ){//污水报告
			var params = MY_SEWAGE_TEST_REPORT.getParams();
			MY_SEWAGE_TEST_REPORT_COM.loadItem(params,$thead,$tbody);
		}
	});

	$('#checkSt').on('change',function(){
		switch (  $('#operateType').val() ){
			case '1':
				var params = MY_SEWAGE_TEST_REPORT.getParams();
				MY_SEWAGE_TEST_REPORT_COM.loadItem(params,$thead,$tbody);
				break;
			case '3':
				var params = MY_SEWAGE_TEST_REPORT.getParams();
				MY_SEWAGE_TEST_REPORT_COM.loadItem(params,$thead,$tbodyReport);
				break;
			case '2':
				MY_SEWAGE.loadItem();
				break;
		}
	});
	//
	// 显示申诉图片
	$('tbody#test_report_tbody').on('click','button[name="showAppealAtta"]',function(){
		var $that = $(this);
		var $parentTr = $that.closest('tr');
		
		var appealAtta = $parentTr.data('appeal_atta');
		var institutionId = $parentTr.data('institution_id');
		// 显示交接单图片
		MY_SHOW_IMG.createShowImgModal(institutionId ,appealAtta);
	});
	
	// 处理申诉 OR 取消异常   注：申诉处理由于多个页面共用，故提炼成appeal.js
	$('tbody#test_report_tbody').on('click','button[name="check"],button[name="closeIllegal"]',function(){
		var $that = $(this);
		var $parentTr = $that.closest('tr');
		
		var illegalId = $parentTr.data('illegal_id');
		var originBtnName= $that.prop('name');
		//清除排序
		var  oTable = document.getElementById("dataTable");
		oTable.sortCol = -1;
		//重置全部排序图标
		$("after").css("opacity",0.3);
		$("before").css("opacity",0.3);
		
		
		var cur_tab = $('label.active_report_tab').prop('id');
		if ( cur_tab == 'instrument_disinfect' ){//消毒器械检测报告
			$(".date2").show();
			$(".date").hide();
			$('#operateType').val('1');
			$('#businessSubType').val('303');
			var params = MY_SEWAGE_TEST_REPORT.getParams();
			if(params){
			// 更新用模态框
			MY_APPEAL.createModalWin
				(illegalId,originBtnName,
					function(){ MY_SEWAGE_TEST_REPORT_COM.loadItem(params,$thead,$tbody); }
				);}
		} else if ( cur_tab == 'sewage_chlorine' ){//污水余氯
			$(".date").show();
			$(".date2").hide();
			$('#operateType').val('2');
			// 更新用模态框
			MY_APPEAL.createModalWin
				(illegalId,originBtnName,
					function(){ MY_SEWAGE.loadItem();}
				);
			
		} else if ( cur_tab == 'sewage_report' ) {//污水报告
			$(".date2").show();
			$(".date").hide();
            $('#operateType').val('3');
            $('#businessSubType').val('301');//查询值
            var params = MY_SEWAGE_TEST_REPORT.getParams();
            if (params) {
                // 更新用模态框
                MY_APPEAL.createModalWin
                (illegalId, originBtnName,
                    function () {
                        MY_SEWAGE_TEST_REPORT_COM.loadItem(params, $thead, $tbody);
                    }
                );
            }
        }
	});
	
	// 显示图片
	$('#test_report_tbody').on('click','button[name="showImage"]',function(){
		var $that = $(this).closest('tr');
		$('#imageModalTitle').text($that.data('institution_name'));
		MY_SHOW_IMG.createShowImgModal($that.data('institution_id'),$that.data('report_img_id'));
	});

	$(".my_date_control" ).datepicker({
		changeMonth: true,
		changeYear: true,
        dateFormat: 'yy-mm-dd',//日期格式  
        yearSuffix: '年', //年的后缀
        showMonthAfterYear:true,//是否把月放在年的后面  
        maxDate: (function() {
        	return todayTxt;
        })(),//最大日期  
        yearRange: (function() {
        	// '1990:2010'
        	var year1 = '1900';
        	var year2 = today.getFullYear();
        	return year1 + ':' + year2;
        })(),
        monthNamesShort: ['01','02','03','04','05','06','07','08','09','10','11','12'],
        dayNamesMin: ['日','一','二','三','四','五','六'],
        onUpdatedDatepicker: function(_year, _month, _calendar) {
        	console.log('onUpdatedDatepicker');
        	var $c = $('div#ui-datepicker-div');
			$c.find('td[data-handler="selectDay"]').each(function(idx, d) {
				var $d = $(d);
				$d.css({'pointer-events': 'auto', 'opacity': 1});
    		});
        }
	});
	
	// 文件下载
	$('#test_report_tbody').on('click','button[name="download"]',function(){
		var $that = $(this);
		var $parent = $that.closest('tr');
    	
    	var data = {};
    	data.reportImgId = $parent.data('report_img_id');
    	data.institutionId = $parent.data('institution_id');
    	data.destFileName = '检测报告';
    	
    	var url = 'invs_multipleDownLoad.do';
    	
    	WeKnow.loading();
    	$.fileDownload(url,
    		{
            httpMethod: 'POST',
            data: data,
            prepareCallback:function(url){
            	console.log('download start')
            	WeKnow.loading();
            },
            successCallback:function(url){
            	console.log('download end')
            	WeKnow.closeLoading();
            },
            failCallback: function (html, url) {
            	WeKnow.errorMsg('下载失败');
            }
         });
	});

	// 页面初期数据查询
	setTimeout(function(){
		$('#instrument_disinfect').click();
	}, 100);
});